package com.ljt.kotlinandmvvm.utils

import android.annotation.SuppressLint
import android.content.Context
import com.ljt.base.http.HttpUrl
import com.ljt.base.utils.LogUtil.d
import com.ljt.kotlinandmvvm.VicApplication
import java.util.*

/**
 * 缓存存储
 */
object SPUtils {
    @SuppressLint("NewApi")
    fun setParam(key: String?, `object`: Any?) {
        try {
            if (`object` != null) {
                val sp = VicApplication.instance.getSharedPreferences(
                    HttpUrl.SP_MODULE_FILE_NAME,
                    Context.MODE_PRIVATE
                )
                val editor = sp.edit()
                if (`object` is String) {
                    //2023.11.17 lujiantao saved
                    //editor.putString(key, AES.encrypt(STRING_MODULE_KEY,(String) object));
                    editor.putString(key, `object` as String?)
                } else if (`object` is Int) {
                    editor.putInt(key, (`object` as Int?)!!)
                } else if (`object` is Boolean) {
                    editor.putBoolean(key, (`object` as Boolean?)!!)
                } else if (`object` is Float) {
                    editor.putFloat(key, (`object` as Float?)!!)
                } else if (`object` is Long) {
                    editor.putLong(key, (`object` as Long?)!!)
                } else {
                    editor.putString(key, `object`.toString())
                }
                editor.apply()
            }
        } catch (e: Exception) {
            d("数据存储异常-->" + e.message)
        }
    }

    fun getParam(key: String?, defaultObject: Any?): Any? {
        try {
            val sp = VicApplication.instance.getSharedPreferences(HttpUrl.SP_MODULE_FILE_NAME, 0)
            return if (defaultObject is String) {
                //2023.11.17 lujiantao saved
                //return AES.decrypt(STRING_MODULE_KEY,sp.getString(key, (String) defaultObject));
                sp.getString(key, defaultObject as String?)
            } else if (defaultObject is Int) {
                sp.getInt(key, (defaultObject as Int?)!!)
            } else if (defaultObject is Boolean) {
                sp.getBoolean(key, (defaultObject as Boolean?)!!)
            } else if (defaultObject is Float) {
                sp.getFloat(key, (defaultObject as Float?)!!)
            } else if (defaultObject is Long) {
                sp.getLong(key, (defaultObject as Long?)!!)
            } else {
                if (defaultObject is Long) sp.getLong(
                    key,
                    (defaultObject as Long?)!!
                ) else sp.getString(key, null as String?)
            }
        } catch (e: Exception) {
            d("缓存数据读取异常-->" + e.message)
        }
        return null
    }

    /**
     * 清理缓存
     */
    fun clearParam() {
        try {
            val sp = VicApplication.instance.getSharedPreferences(HttpUrl.SP_MODULE_FILE_NAME, 0)
            val editor = sp.edit()
            editor.clear()
            editor.commit()
        } catch (e: Exception) {
            d("缓存数据读取异常-->" + e.message)
        }
    }

    /**
     * 保存List
     *
     * @param tag
     * @param datalist
     */
    fun setListBeans(tag: String?, datalist: ArrayList<String?>) {
        saveListToSP(datalist, tag)
    }

    /**
     * 获取List
     *
     * @param tag
     * @return
     */
    fun getListBeans(tag: String?): ArrayList<String> {
        val sp = VicApplication.instance.getSharedPreferences(HttpUrl.SP_MODULE_FILE_NAME, 0)
        val spStr = sp.getString(tag, "")
        val list = ArrayList<String>()
        for (str in spStr!!.split(",").toTypedArray()) {
            list.add(str)
        }
        return list
    }

    /**
     * 存储list<String>
     *
     * @param list
     * @param tag
    </String> */
    fun saveListToSP(list: ArrayList<String?>, tag: String?) {
        val sp = VicApplication.instance.getSharedPreferences(HttpUrl.SP_MODULE_FILE_NAME, 0)
        val editor = sp.edit()
        val spStr = StringBuilder()
        for (str in list) {
            spStr.append(str).append(",")
        }
        editor.putString(tag, if (list.size == 0) "" else spStr.substring(0, spStr.length - 1))
        editor.apply()
    }
}